package core.util;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Locations implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = 8877924924131634181L;

    private Set<String> locations = new HashSet<String>();

    StringBuffer strLocations = new StringBuffer();

    public Locations( String locations ) {
        for( String location : locations.split( "," ) ) {
            this.locations.add( location );
        }
    }
    
    public Locations( Set<String> locationSet ) {
    	if(locations.size() == 0){
    		throw new IllegalArgumentException("Must have at least 1 location");
    		// just preserving your invariant, it case it was important
    	}
    	locations.addAll(locationSet);
    }

    public Locations() {
    }

    public void add( String location ) {
        if( !locations.isEmpty() ) {
            strLocations.append( "," );
        }
        strLocations.append( location );
        locations.add( location );
    }

    public boolean contains( String location ) {
        return locations.contains( location );
    }

    public String toString() {
        return strLocations.toString();
    }

    @Override
    public boolean equals( Object obj ) {
        if( this == obj )
            return true;
        if( obj == null )
            return false;
        if( getClass() != obj.getClass() )
            return false;
        Locations loc = (Locations) obj;
        return this.locations.equals( loc.locations );
    }

    public Iterator<String> iterator() {
        return locations.iterator();
    }

    /**
     * Performs a deep copy, then returns the copy
     * 
     * @return a copy of all locations
     */
    public Set<String> getValues() {
        Set<String> newLocations = new HashSet<String>();
        for( String location : this.locations ) {
            newLocations.add( location );
        }
        return newLocations;
    }

}